<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        ul {
            width: 1200px;
            margin: auto;
            display: flex;
            flex-wrap: wrap;
        }

        li {
            width: 23%;
            margin: 1%;
            box-shadow: 0 0 1px 1px #ddd;
            text-align: center;
        }

        img {
            width: 100%;
            height: 300px;
        }
    </style>
</head>

<body>
    <div class="orderBox">
        <select class="orderPrice">
            <option value="1">按照价格升序</option>
            <option value="2">按照价格降序</option>
        </select>

        <select class="orderXL">
            <option value="1">按照销量升序</option>
            <option value="2">按照销量降序</option>
        </select>

        <!-- <button class="orderBtn">搜索</button> -->
    </div>
    <ul>
        <!-- <li>
            <img src="" alt="">
            <h3></h3>
            <p></p>
            <strong></strong>
        </li> -->
    </ul>


    <script src="../js/ajax.js"></script>
    <script>
        const oUl = document.querySelector('ul');

        // 页面打开的时候渲染所有的数据
        render('../php/goods_list.php');


        // 排序
        // const oOrder = document.querySelector('.order');
        const oPrice = document.querySelector('.orderPrice');
        const oXL = document.querySelector('.orderXL');

        oPrice.onchange = function () {
            const orderType = oPrice.value;  // 1 0

            // goods_price 必须和数据库中的字段保持一致  
            render(`../php/order.php?orderName=goods_price&orderType=${orderType}`);
        }

        oXL.onchange = function () {
            const orderType = oXL.value;  // 1 0

            // goods_price 必须和数据库中的字段保持一致  
            render(`../php/order.php?orderName=goods_l&orderType=${orderType}`);
        }




        function render(path) {
            ajax({
                // path:path,
                path,
                success: res => {
                    console.log(res);
                    const { status, msg, data } = res;
                    if (status) {
                        let html = '';
                        data.forEach(v => {
                            const { goods_title, goods_des, goods_price, goods_img, goods_id } = v;
                            html += `
                    <li>
                        <a href="./04detail.html?id=${goods_id}">
                            <img src="${goods_img}" alt="">
                            <h3>${goods_title}</h3>
                            <p>${goods_des}</p>
                            <strong>${goods_price}</strong>
                        </a>
                    </li>
                `
                        })
                        oUl.innerHTML = html;
                    } else {
                        alert(msg);
                    }
                }
            })
        }


    </script>

</body>

</html>